<template>
  <div>
    <!-- 卡片视图区 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <div class="header">
            <el-input @change="searchInp" v-model="input" placeholder="请输入内容" icon="el-icon-search"> <el-button slot="append" icon="el-icon-search"></el-button></el-input>
          </div>
        </el-col>
      </el-row>
      <!-- 订单列表数据 -->
      <el-table :data="tableData" border stripe>
        <el-table-column type="index" label="#" width="100"></el-table-column>
        <el-table-column label="订单编号" prop="order_name" width="140" show-overflow-tooltip></el-table-column>
        <el-table-column label="订单价格" prop="price" width="100"></el-table-column>
        <el-table-column label="订单重量" prop="order_weight" width="100"></el-table-column>
        <el-table-column label="订单数量" prop="num" width="100"></el-table-column>
        <el-table-column label="是否付款" prop="status" width="100">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.status === 1">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" width="100"></el-table-column>
        <el-table-column label="下单时间" prop="order_time">
          <template slot-scope="scope">
            {{ scope.row.order_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="地址" prop="address"></el-table-column>
        <el-table-column label="操作">
          <template>
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showBox()"></el-button>
            <el-button type="success" icon="el-icon-location" size="mini" @click="showProgressBox"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <div style="text-align: center; margin: 20px">
        <el-pagination @size-change="handleSizeChange" background layout="total, prev,sizes, pager, next, jumper" :current-page.sync="currentNo" :page-sizes="[6, 10, 15, 20]" @current-change="currentPage" :page-size="pageCount" :total="totalPage"> </el-pagination>
      </div>
    </el-card>
    <!-- 修改地址的对话框 -->
    <el-dialog title="修改地址" :visible.sync="addressDialogVisible" width="50%" @close="addressClosed">
      <el-form :model="addressForm" :rules="addressFormRules" ref="addressFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader v-model="addressForm.address1" :options="cityData" :props="{ expandTrigger: 'hover' }" @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 展示物流进度的对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressDialogVisible" width="50%">
      <!-- 时间线 -->
      <el-timeline>
        <el-timeline-item v-for="(activity, index) in progressInfo" :key="index" :timestamp="activity.time">
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="progressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="progressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata'
export default {
  name: 'FirstProjectOrder',

  data() {
    return {
      index: '',
      input: '',
      pageNo: 1, //表格页码
      pageCount: 6, //数据数
      currentNo: 1, //当前页码
      totalPage: 1,
      type: 1,
      list: [],
      tableData: [],
      addressDialogVisible: false,
      addressForm: {
        address1: [],
        address2: ''
      },
      addressFormRules: {
        address1: [
          {
            require: true,
            message: '请选择省市区县',
            trigger: 'blur'
          }
        ],
        address2: [
          {
            require: true,
            message: '请填写详细地址',
            trigger: 'blur'
          }
        ]
      },
      cityData,
      progressDialogVisible: false,
      progressInfo: []
    }
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get('/order/getOrderList')
      // this.tableData = res.data
      this.totalPage = res.total

      // console.log(res)
    },
    // 每页条数
    handleSizeChange(val) {
      this.pageCount = val
      this.currentNo = 1
      this.getData()
      console.log(`每页 ${val} 条`)
    },
    // 展示修改地址的对话框
    showBox() {
      this.addressDialogVisible = true
    },
    handleChange() {
      console.log(this.addressForm.address1)
    },
    addressClosed() {
      this.$refs.addressFormRef.resetFields()
    },
    async showProgressBox() {
      this.progressInfo = [
        {
          time: '派送中 07-12 10:36',
          context: '[九江市]配送员小陈 1**********正在为您派送，感谢您的耐心等待。'
        },
        {
          time: '07-07 07-42',
          context: '[九江市]订单已到达[江西九江公司]'
        },
        {
          time: '07-07 01:09',
          context: '[南昌市]快件已由江西南昌运转中心出发，正在发往江西南昌运转中心'
        },
        {
          time: '07-06 23:40',
          context: '[南昌市]订单已到达[江西南昌运转中心]'
        },
        {
          time: '07-05 18:32',
          context: '[重庆市]快件已从重庆运转中心出发,正在发往江西南昌运作中心'
        },
        {
          time: '已揽收 17:23',
          context: '[重庆市]您的包裹由物流公司揽收'
        },
        {
          time: '已发货 07-05 08:57',
          context: '包裹正在等待揽收'
        },
        {
          time: '已出库 07-05 08:55',
          context: '包裹已出库'
        },
        {
          time: '仓库处理中 07-04 22:52',
          context: '打包完成'
        },
        {
          time: '仓库已接单 07-04 08:44',
          context: '您的订单已入库'
        },
        {
          time: '商品已下单 07-04 19:52',
          context: '您的订单预计2022-07-05 18:00:00前出库,请您耐心等待'
        }
      ]

      this.progressDialogVisible = true
    },
    //获取前4条数据
    getData(index) {
      this.pageNo = index || this.pageNo
      this.$http
        .post('/order/page', { page: this.pageNo, pageSize: this.pageCount })
        .then(res => {
          this.tableData = res.data.data
          console.log(res.data.data)
        })
        .catch(error => {
          console.log(error)
        })
    },
    //当前页数据
    currentPage(currentPage) {
      this.currentNo = currentPage
      console.log(this.currentNo)
      // this.getData(this.currentNo)
      if (this.type == 1) {
        this.getData(this.currentNo)
      } else {
        this.tableData = this.list.slice((this.currentN - 1) * 3, this.currentN * 3)
      }
    },
    // 搜索
    async searchInp(val) {
      const { data: res } = await this.$http.get('/order/search', { params: { search: val } })
      console.log(res)
      this.currentNo = 1
      if (res.code === 0) {
        this.list = res.data
        this.tableData = []
        this.pageCount = 3
        this.tableData = res.data.slice(0, 3)
        this.totalPage = res.total
        this.type = 2
      } else {
        this.tableData = []
        this.totalPage = 1
        this.pageCount = 1
        this.type = 1
      }
    }
  },
  mounted() {
    this.getOrderList()
    this.getData()
  },
  created() {}
}
</script>

<style lang="scss" scoped>
.el-cascader {
  width: 100%;
}
.header {
  display: flex;
}
</style>
